﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace MefTZAddin
{
    public class TaskDialogSupport
    {
        static public bool MessagePrompt(TimeZoneInfo tz)
        {
            // Common buttons sample
            TaskDialog tdCommonButtons = new TaskDialog();
            tdCommonButtons.Cancelable = false;
            tdCommonButtons.Caption = "Time zone change";
            tdCommonButtons.Text = "You have entered the " + tz.DisplayName + " time zone";
            tdCommonButtons.InstructionText = "Do you want to update to this time zone?";

            tdCommonButtons.StandardButtons =
                TaskDialogStandardButtons.Yes |
                TaskDialogStandardButtons.No;

            TaskDialogResult tdr = tdCommonButtons.Show();
            return (tdr == TaskDialogResult.Yes);
        }

    }

    public class TimezonePickerDialog : TaskDialog
    {
        private EventHandler clickHandler;

        public TimeZoneInfo SelectedTimezone
        { get; set; }

        public TimezonePickerDialog()
        {
            this.Caption = "Time zone change";
            this.Text = "Your current location matches more than one potential time zone.";
            this.InstructionText = "Please select the correct time zone or click Cancel:";
            this.FooterText = "Time zone changer -- MEF Utility Runner";
            this.Cancelable = true;

            // Add a close button so user can close our dialog
            this.StandardButtons = TaskDialogStandardButtons.Cancel;

            clickHandler = new EventHandler(tdcl_Click);
        }

        public TimeZoneInfo Prompt(TimeZoneInfo[] zones)
        {
            SelectedTimezone = null;
            this.Controls.Clear();

            //using (new EnableTaskDialogInScope(true))
            {
                #region Creating and adding command link buttons

                foreach (TimeZoneInfo tz in zones)
                {
                    TaskDialogCommandLink tdcl = new TaskDialogCommandLink(tz.Id, tz.DisplayName);
                    tdcl.Click += clickHandler;

                    this.Controls.Add(tdcl);
                }
                #endregion

                // Show the taskdialog
                TaskDialogResult result = this.Show();
                if (result == TaskDialogResult.Cancel) return null;
                else return SelectedTimezone;
            }
        }


        void tdcl_Click(object sender, EventArgs e)
        {
            TaskDialogCommandLink tdcl = sender as TaskDialogCommandLink;
            if (tdcl != null)
            {
                SelectedTimezone = TimeZoneInfo.FindSystemTimeZoneById(tdcl.Name);
                this.Close(TaskDialogResult.Ok);
            }
        }

    }
}
